﻿defClass(happyfarm.util, "Request", null, function(Request, Request_prototype){
    var myRequest = null;
    var progIDs = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
    
    Request_prototype.init = function(){
    	this.tools = new happyfarm.util.Tools();
    };
    
    Request_prototype.getXMLHttpRequest = function(){
        //IE浏览器
        if (navigator.userAgent.indexOf("MSIE") > 0) {
            //IE7/IE8
            if (window.XMLHttpRequest) {
                myRequest = new XMLHttpRequest();
            } else {
                //IE6及以下版本
                for (var i = 0; i < progIDs.length; ++i) {
                    try {
                        myRequest = new ActiveXObject(progIDs[i]);
                    } catch (e) {
                        //alert(e);
                    }
                }
            }
        } else if (navigator.userAgent.indexOf("Firefox") > 0) {
            //Firefox浏览器
            //打开跨域访问权限
            netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
            myRequest = new XMLHttpRequest();
        }
        
        return myRequest;
    };
    
    //POST方法
    Request_prototype.doPost = function(postData, mode, mod, act, flag, uinX){
        var tools = new happyfarm.util.Tools();
        
        var xmlhttp = this.getXMLHttpRequest();
        // firefox,打开跨域访问权限
        if (window.navigator.userAgent.indexOf("Firefox") >= 1) {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
        }
        try {
			var url = "";
			if (mode != null) {
				url = host + mode;
			}
            if (mod != null) {
                url += "&" + "mod=" + mod;
            }
            if (act != null) {
                url += "&" + "act=" + act;
            }
            if (flag != null) {
                url += "&" + "flag=" + flag;
            }
            xmlhttp.open("POST", url, false);
            xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			if (postData == null) {
                postData = "onlineTime=0" + "&" + "uIdx=" + myId + "&" + "uinY=" + uinLogin + "&" + this.tools.getKey() + "&" + "uinY=" + uinLogin;
            } else {
                postData = "onlineTime=0" + "&" + postData + "&uIdx=" + myId + "&" + "uinY=" + uinLogin + "&" + this.tools.getKey();
            }
			if(validateCode){
				postData += "&validatemsg=" + validateCode;
			}
            xmlhttp.send(postData);
            return xmlhttp.responseText;
        } catch (error) {
            //alert("网络连接超时");
            return;
        }
    };
    
    //GET方法
    Request_prototype.doGet = function(mod, act, flag, ownerId){
        var tools = new happyfarm.util.Tools();
        
        var xmlhttp = this.getXMLHttpRequest();
        // firefox,打开跨域访问权限
        if (window.navigator.userAgent.indexOf("Firefox") >= 1) {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
        }
        try {
            url = host + "api.php?mod=" + mod;
            if (act != null) {
                url += "&" + "act=" + act;
            }
            if (flag != null) {
                url += "&" + "flag=" + flag;
            }
            if (ownerId != null) {
                url += "&" + "ownerId=" + ownerId;
            }
			url = url + "&validatemsg=" + validateCode;
            xmlhttp.open("GET", url, false);
            xmlhttp.send(null);
            return xmlhttp.responseText;
        } catch (error) {
            //alert(error);
            return;
        }
    };
    
    Request_prototype.doGetCommon = function(url, fct){
        var xmlhttp = this.getXMLHttpRequest();
        // firefox,打开跨域访问权限
        if (window.navigator.userAgent.indexOf("Firefox") >= 1) {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
        }
        try {
            if (fct == null) {
                xmlhttp.open("GET", url, false);
            } else {
                xmlhttp.onreadystatechange = function(){
                    fct(xmlhttp);
                };
            	xmlhttp.open("GET", url, true);
            }
            xmlhttp.send(null);
            return xmlhttp.responseText;
        } catch (error) {
            //alert("网络连接超时");
            return;
        }
    };
	
	Request_prototype.doPostCommon = function(postData, url, mod, act, flag){
        var xmlhttp = this.getXMLHttpRequest();
        // firefox,打开跨域访问权限
        if (window.navigator.userAgent.indexOf("Firefox") >= 1) {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
        }
        try {
            if (mod != null) {
                url += "mod=" + mod;
            }
            if (act != null) {
                url += "&" + "act=" + act;
            }
            if (flag != null) {
                url += "&" + "flag=" + flag;
            }
			url += "&validatemsg=" + validateCode;
            xmlhttp.open("POST", url, false);
            xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			if (postData == null) {
                postData = "uIdx=" + myId + "&" + "uinY=" + uinLogin + "&" + this.tools.getKey() + "&validatemsg=" + validateCode;
            } else {
                postData += "&uIdx=" + myId + "&" + "uinY=" + uinLogin + "&" + this.tools.getKey() + "&validatemsg=" + validateCode;
            }
            xmlhttp.send(postData);
            return xmlhttp.responseText;
        } catch (error) {
            //alert(error);
            return;
        }
    };
    
    Request_prototype.doGetRequest = function(mode, mod, act, flag, ownerId, uinX, fct){
		var me = this;
        var xmlhttp = this.getXMLHttpRequest();
        // firefox,打开跨域访问权限
        if (window.navigator.userAgent.indexOf("Firefox") >= 1) {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
        }
        try {
			var url = host + mode + "mod=" + mod;
            if (act != null) {
                url += "&" + "act=" + act;
            }
            if (flag != null) {
                url += "&" + "flag=" + flag;
            }
			if (ownerId != null) {
                url += "&" + "ownerId=" + ownerId;
            }
			if (uinX != null) {
                url += "&" + "uinX=" + uinX;
            }
            url = url + "&" + "uinY=" + uinLogin + "&validatemsg="+validateCode;
            if (fct == null) {
                xmlhttp.open("GET", url, false);
            } else {
                xmlhttp.onreadystatechange = function(){
					if (xmlhttp.readyState == 4) {
						fct(xmlhttp);
					}
                };
            	xmlhttp.open("GET", url, true);
            }
            xmlhttp.send(null);
			
            return xmlhttp;
        } catch (error) {
            //alert(error);
            return;
        }
    };
    
    Request_prototype.doPostRequest = function(postData, mode, mod, act, flag, uinX, fct){
        var me = this;
        var xmlhttp = this.getXMLHttpRequest();
        // firefox,打开跨域访问权限
        if (window.navigator.userAgent.indexOf("Firefox") >= 1) {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
        }
        try {
            var url = host + mode + "mod=" + mod;
            if (act != null) {
                url += "&" + "act=" + act;
            }
            if (flag != null) {
                url += "&" + "flag=" + flag;
            }
			if(validateCode){
            	url += "&validatemsg="+validateCode;
			}
            if (fct == null) {
                xmlhttp.open("POST", url, false);
            } else {
                xmlhttp.onreadystatechange = function(){
					if (xmlhttp.readyState == 4) {
						fct(xmlhttp);
					}
                };
                xmlhttp.open("POST", url, true);
            }
            xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            if (postData == null) {
                postData = "uIdx=" + myId + "&" + "uinX=" + uinX + "&" + "uinY=" + uinLogin + "&" + this.tools.getKey();
            } else {
                postData = postData + "&uIdx=" + myId + "&" + "uinX=" + uinX + "&" + "uinY=" + uinLogin + "&" + this.tools.getKey();
            }
			if(validateCode){
				postData += "&validatemsg=" + validateCode;
			}
            xmlhttp.send(postData);
			postData = postData + "&f=-1";
			this.doPostCommon(postData, "cgi_farm_checkStatus?", "user", "checkStatus", flag, uinX);
			return xmlhttp;
        } catch (error) {
            //alert("网络连接超时");
            return;
        }
    };
    
});
